package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.common.SelectOperatorAdapter;
import com.inossem.warehousestorage_inossem.bean.common.GetOperatorBean;
import com.inossem.warehousestorage_inossem.constant.TypeEnum;
import com.inossem.warehousestorage_inossem.listener.GetOperatorListener;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.common.GetOperatorRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;

import java.util.ArrayList;
import java.util.List;

public class InStorageOtherSelectOperatorActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView receiverList;

    private SelectOperatorAdapter adapter;

    private String conditionStr, roleCodeStr;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_receive_select_receiver);
        initView(InStorageOtherSelectOperatorActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(InStorageOtherSelectOperatorActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.receiverListRefresh);
        receiverList = (PullableListView) activity.findViewById(R.id.receiverList);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_select_receiver_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_add_receiver_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        receiverList.setPullUpFlag(false);
        receiverList.setPullDownFlag(false);
        getExtra(activity);
        getData(activity);
    }

    private void getExtra(Activity activity) {
        conditionStr = activity.getIntent().getStringExtra(InStorageExtra.IN_STORAGE_CONDITION);
        roleCodeStr = activity.getIntent().getStringExtra(InStorageExtra.IN_STORAGE_ROLE);
    }

    private void getData(Context context) {
        GetOperatorRequest.request(context, conditionStr, roleCodeStr, TypeEnum.IN_STORAGE_OTHER, new GsonBeanListener<GetOperatorBean>() {
            @Override
            public void onSuccess(GetOperatorBean bean) {
                List<GetOperatorBean.OperatorBean> allReceiverList = bean.getBody();
                List<GetOperatorBean.OperatorBean> currentReceiverList = InStorageUtils.getInStorageOtherBean(context).getUser_list();
                List<GetOperatorBean.OperatorBean> resultReceiverList = filterReceiver(allReceiverList, currentReceiverList);
                addReceiverListData(context, resultReceiverList, receiverList);
            }
        });
    }

    private List<GetOperatorBean.OperatorBean> filterReceiver(List<GetOperatorBean.OperatorBean> allReceiverList, List<GetOperatorBean.OperatorBean> currentReceiverList) {
        if (currentReceiverList == null) {
            return allReceiverList;
        }
        List<GetOperatorBean.OperatorBean> removeReceiverList = new ArrayList<>();
        for (int i = 0; i < allReceiverList.size(); i++) {
            GetOperatorBean.OperatorBean receiverItem = allReceiverList.get(i);
            for (int j = 0; j < currentReceiverList.size(); j++) {
                if (receiverItem.getUser_id().equals(currentReceiverList.get(j).getUser_id())) {
                    removeReceiverList.add(receiverItem);
                }
            }
        }
        allReceiverList.removeAll(removeReceiverList);
        return allReceiverList;
    }


    private void addReceiverListData(Context context, List<GetOperatorBean.OperatorBean> selectReceiverList, ListView listView) {
        adapter = new SelectOperatorAdapter(context, selectReceiverList, new GetOperatorListener() {
            @Override
            public void onSelectReceiver(GetOperatorBean.OperatorBean bean) {
                InStorageUtils.addOtherOperator(context, bean);
                InStorageOtherSelectOperatorActivity.this.finish();
            }
        });
        receiverList.setAdapter(adapter);
    }

    public interface ReceiveSelectReceiverListener {
        void onSelectReceiver();
    }

}
